<template>
  <div class="accountContainer">
    <div class="option">
      <el-button
        type="success"
        size="small"
        @click="addPerson()"
      >新增人员</el-button>
      <el-button
        type="primary"
        size="small"
        @click="Refresh()"
      >刷新</el-button>
    </div>
    <el-table
      :data="List"
      border
      style="width: 100%"
    >
      <el-table-column
        prop="index"
        label="序号"
      ></el-table-column>
      <el-table-column
        prop="account"
        label="账号"
      ></el-table-column>
      <el-table-column label="头像">
        <template slot-scope="scope">
          <img
            :src="`http://www.jfqiandai.com/file${scope.row.avatar}`"
            style="width:80px"
          >
        </template>
      </el-table-column>
      <el-table-column
        prop="email"
        label="电子邮件"
      ></el-table-column>
      <el-table-column
        prop="name"
        label="名字"
      ></el-table-column>
      <el-table-column
        prop="phone"
        label="电话"
      ></el-table-column>
      <el-table-column
        prop="createTime"
        label="创建时间"
      ></el-table-column>
      <el-table-column
        prop="logintime"
        label="登录时间"
      ></el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        width="240"
      >
        <template slot-scope="scope">
          <el-button
            @click="EditClick(scope.row)"
            type="primary"
            plain
            size="small"
          >编辑</el-button>
          <el-button
            @click="DeleteClick(scope.row)"
            type="danger"
            size="small"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="paginat">
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage4"
        :page-size="10"
        layout="total, prev, pager, next"
        :total="total"
      ></el-pagination>
    </div>

    <!-- 弹框 -->
    <el-dialog
      title="提示"
       @close="Cancel"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <el-form
        :model="ruleForm"
        ref="ruleForm"
        label-width="70px"
        class="demo-ruleForm"
      >
        <el-form-item
          label="账 号"
          :rules="[
          { required: true, message: '请输入账号'}]"
        >
          <el-input v-model="ruleForm.account"></el-input>
        </el-form-item>

        <el-form-item
          label="密 码"
          :rules="[
          { required: true, message: '请输入密码'}]"
          v-show="flag == 1"
        >
          <el-input
            v-model="ruleForm.password"
            type="password"
          ></el-input>
        </el-form-item>

        <el-form-item
          label="重置密码"
          prop="email"
          v-show="flag == 2"
        >
          <el-input
            v-model="newPass"
            placeholder="请填写需要重置的密码"
          ></el-input>
        </el-form-item>

        <el-form-item
          label="头像"
          prop="avatar"
        >
          <el-upload
            list-type="picture-card"
            action="http://www.jfqiandai.com/img/upload"
            :file-list="fileList"
            :on-success="onSuccess"
            :on-remove="handleRemove"
            :limit="1"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>

        <el-form-item
          label="邮 箱"
          prop="email"
        >
          <el-input v-model="email"></el-input>
        </el-form-item>

        <el-form-item
          label="名 字"
          prop="account"
        >
          <el-input v-model="name"></el-input>
        </el-form-item>

        <el-form-item
          label="电 话"
          prop="account"
        >
          <el-input v-model="phone"></el-input>
        </el-form-item>

        <div style="text-align:right;">
          <el-button @click="Cancel">取 消</el-button>
          <el-button
            type="primary"
            @click="submitForm('ruleForm')"
          >确定</el-button>
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>


<script>
export default {
  data() {
    return {
      currentPage4: 4,
      List: [],
      total: 0,
      dialogVisible: false,
      fileList: [],
      flag: 0, //判断是 编辑还是添加的标识
      editId: 0, //编辑id
      val: 1,

      // account: "", //账号
      // password: "", //密码
      newPass: "", //重置的密码
      avatar: "", //头像
      email: "", //电子邮件
      name: "", //名字
      phone: "", //电话

      ruleForm: {
        account: "", //账号
        password: "" //密码
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //确定
    submitForm(formName) {
      if (this.flag == 1) {
        this.$refs[formName].validate(valid => {
          if (valid) {
            let url = "/admin/add";
            let params = {
              account: this.ruleForm.account,
              password: this.ruleForm.password,
              avatar: this.avatar,
              email: this.email,
              name: this.name,
              phone: this.phone
            };
            console.log(params);
            this.httpPost(url, params, res => {
              if (res.code == 200) {
                this.dialogVisible = false;
                this.getList();
              }
            });
          } else {
            console.log("error submit!!");
            return false;
          }
        });
      } else {
        let url = "/admin/edit";
        let params = {
          id: this.editId,
          account: this.ruleForm.account,
          password: this.newPass,
          avatar: this.avatar,
          email: this.email,
          name: this.name,
          phone: this.phone
        };
        console.log(params);
        this.httpPut(url, params, res => {
          if (res.code == 200) {
            this.dialogVisible = false;
            this.getList();    
          }
        });
      }
    },
             //取消
    Cancel() {
      this.dialogVisible = false;
      this.fileList = [];
    },
    //新增
    addPerson() {
      this.dialogVisible = true;
      this.flag = 1;
      this.ruleForm.account = "";
      this.ruleForm.password = "";
      this.avatar = "";
      this.fileList = [];
      this.email = "";
      this.name = "";
      this.phone = "";
    },

    //编辑
    EditClick(row) {
      this.flag = 2;
      this.editId = row.id;
      this.ruleForm.account = row.account;
      if (row.avatar) {
        this.avatar = row.avatar;
        this.fileList.push({ url: "http://www.jfqiandai.com/file" + this.avatar });
      }else{
        this.fileList = []
      }
      this.email = row.email;
      this.name = row.name;
      this.phone = row.phone;
      // setTimeout(()=>{
              this.dialogVisible = true;
      // },1000)
    },

    //图片上传
    onSuccess(Response, file, fileList) {
      this.avatar = file.response.data.path;
    },
    handleRemove(file, fileList) {
      this.avatar = "";
      // this.fileList[0] = {};
    },
    //刷新
    Refresh() {
      this.getList();
    },
    //删除
    DeleteClick(row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let url = "/admin/edit";
          let params = {
            id: row.id,
            status: 3
          };
          this.httpPut(url, params, res => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: "删除成功"
              });
              this.dialogVisible = false;
              this.getList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    //分页
    handleCurrentChange(val) {
      this.val = val;
      this.getList();
      console.log(`当前页: ${val}`);
    },
    //查数据
    getList() {
      let url = "/admin/getList";
      let params = {
        pageNum: this.val,
        pageSize: 10
      };
      this.httpPost(url, params, res => {
        if (res.code == 200) {
          this.total = res.data.total;
          this.List = res.data.list;
          this.List.forEach((element, i) => {
            this.List[i].index = (this.val - 1) * 10 + i + 1;
          });
        } else {
          console.log("查询失败");
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
//  账号管理
.accountContainer {
  // position: relative;
  .option {
    padding: 10px;
  }
  //弹框
  .row {
    padding: 10px;
    .rowTil {
      display: block;
      padding: 10px 0;
    }
  }
}
</style>
